-only_company = params[:company] == 'true'
-only_regulation = params[:company] == 'false'

-content_for :modal_title do
  -object_type_name = 'control'
  -object_type_name = 'company control' if only_company
  -object_type_name = 'regulation control' if only_regulation
  -if f.object.new_record?
    Create a new #{object_type_name}
  -else
    Edit #{object_type_name}

-if !f.object.new_record?
  -content_for :modal_delete_path, delete_flow_control_path(f.object)

.row-fluid
  =wrapped_text_field f, :span4, :title, :placeholder => 'Enter title', :label_name => 'Title <span class="required">*</span>'.html_safe
  -if f.object.new_record? && params[:directive_id].present?
    -f.object.directive_id = params[:directive_id]
  -if f.object.new_record? && f.object.directive_id.present?
    .span2
      =f.label 'Directive'
      =f.hidden_field :directive_id
      =f.object.directive.slug
  -else
    -directives = Directive
    -directives = directives.where(:company => true) if only_company
    -directives = directives.where(:company => false) if only_regulation
    -# Ensure the specified directive is included?
    -#directives.unshift(f.object.directive) if f.object.directive && !directives.include?(f.object.directive)
    -if params[:program_id].present?
      =hidden_field_tag :program_id, params[:program_id]
      -directives = directives.joins(:program_directives).where(:program_directives => { :program_id => params[:program_id] })
    -else
      -directives = directives.all_company_controls_first
      -directives = directives.reject(&:is_stealth_directive?)
    -if directives.size > 1
      =wrapped_select f, :span2, :directive_id, directives.map { |p| [p.slug, p.id] }, :selected => f.object.directive_id, :include_blank => false
    -elsif directives.size == 1
      .span2
        =#f.label 'Directive'
        =f.hidden_field :directive_id, :value => directives[0].id
        =#directives[0].slug
    -else
      .span2.alert.alert-error
        Directive required to add Controls
      -content_for :modal_save_button do
        %input.btn.btn-primary.preventdoubleclick.disabled{:type => "button", :href => 'javascript://', :disabled => true, :value=> "Save" } 

  .span6
    .row-fluid
      =wrapped_text_field f, :span4, :slug, :label_name => 'Code (autofill)', :placeholder => 'CONTROL-XXXX'
      -#=wrapped_text_field f, :span1, :version, :placeholder => '1.1.1'
      =wrapped_date_field f, :span4, :start_date, :class => 'date', :label_name => 'Effective Date', :placeholder => 'MM/DD/YYYY'
      =wrapped_date_field f, :span4, :stop_date, :class => 'date', :label_name => 'Stop Date', :placeholder => 'MM/DD/YYYY'

.row-fluid
  =wrapped_text_area f, :span6, :description, :class => 'double wysihtml5', :wrapper_div_class => 'wysiwyg-area', :id => "text1", :placeholder => 'Enter description here', :label_name => 'Description <span class="required">*</span>'.html_safe
  .span6
    .row-fluid
      -#=wrapped_select(f, :span6, :type_id, ['Regulation', 'Company'], :selected => f.object.company? ? 'Company' : 'Regulation', :)
      =wrapped_select(f, :span6, :kind_id, Option.options_with_none_for(:control_kind).map { |o| [o.title, o.id] }, :selected => (f.object.respond_to?(:kind_id) ? f.object.kind_id : nil))
    .row-fluid
      .span6
        =f.label 'Fraud-related'
        %select.span12{ :name => 'control[fraud_related]' }
          %option{ :value => nil, :selected => f.object.fraud_related.nil? && 'selected' }
          %option{ :value => '0', :selected => f.object.fraud_related === false && 'selected' } No
          %option{ :value => '1', :selected => f.object.fraud_related && 'selected' } Yes
        =error_messages_inline(f, :fraud_related)
      .span6
        =f.label 'Significance'
        %select.span12{ :name => 'control[key_control]' }
          %option{ :value => nil, :selected => f.object.fraud_related.nil? && 'selected' }
          %option{ :value => '0', :selected => f.object.fraud_related === false && 'selected' } Non-key
          %option{ :value => '1', :selected => f.object.key_control && 'selected' } Key
        =error_messages_inline(f, :key_control)

.row-fluid
  =wrapped_text_field f, :span6, :url, :label_name => 'Direct Link', :placeholder => 'http://www.example.com/'
  .span6
    .row-fluid
      =wrapped_select(f, :span6, :means_id, Option.options_with_none_for(:control_means).map { |o| [o.title, o.id] }, :selected => (f.object.respond_to?(:means_id) ? f.object.means_id : nil))
      =wrapped_select(f, :span6, :verify_frequency_id, Option.options_with_none_for(:verify_frequency).map { |o| [o.title, o.id] }, :selected => (f.object.respond_to?(:verify_frequency_id) ? f.object.verify_frequency_id : nil), :label_name => 'Frequency')

%br

.row-fluid
  .span6{ :class => error_class(f, :documentation_description) }
    .row-fluid
      =wrapped_text_area(f, :span12, :documentation_description, :placeholder => "Description of documentation", :rows => "3", :class => "double wysihtml5", :wrapper_div_class => 'wysiwyg-area')

  #assertionlist.span3{ :class => error_class(f, :assertions) }
    %label{:for => 'control-assertions'} Assertions
    -assertion_list = Category.ctype(Control::CATEGORY_ASSERTION_TYPE_ID).order('lft ASC').all
    %select{:multiple => "multiple", :name => 'control[assertion_ids][]'}
      -assertion_list.each do |category|
        %option{ :value => category.id, :selected => (f.object.assertion_ids.include?(category.id)) }
          #{('&nbsp;&nbsp;&nbsp;' * category.depth).html_safe}#{category.name}
    =error_messages_inline(f, :assertions)

  #categorylist.span3{ :class => error_class(f, :categories) }
    %label{:for => 'control-categories'} Categories
    -category_tree = Category.ctype(Control::CATEGORY_TYPE_ID).order('lft ASC').all
    %select{:multiple => "multiple", :name => 'control[category_ids][]'}
      -category_tree.each do |category|
        %option{ :value => category.id, :selected => (f.object.category_ids.include?(category.id)) }
          #{('&nbsp;&nbsp;&nbsp;' * category.depth).html_safe}#{category.name}
    =error_messages_inline(f, :categories)

.row-fluid
  .span6{ :class => error_class(f, :notes) }
    .row-fluid
      =wrapped_text_area(f, :span12, :notes, :placeholder => "Enter notes here", :rows => "3", :class => "double wysihtml5", :wrapper_div_class => 'wysiwyg-area')

=render :partial => "shared/wysihtml5"
